package com.sinya.shoppingmall.fragment;

import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.refresh.library.pulltorefreshload.PullToRefreshBase;
import com.refresh.library.swipetoloadlayout.OnLoadMoreListener;
import com.refresh.library.swipetoloadlayout.OnRefreshListener;
import com.refresh.library.swipetoloadlayout.SwipeToLoadLayout;
import com.sinya.shoppingmall.R;
import com.sinya.shoppingmall.bean.BaseResult;
import com.sinya.shoppingmall.bean.Constants;
import com.sinya.shoppingmall.bean.ProductBean;
import com.sinya.shoppingmall.enums.SortType;
import com.sinya.shoppingmall.https.VolleyParams;
import com.sinya.shoppingmall.https.VolleyRequest;
import com.sinya.shoppingmall.implement.GetDataInterface;
import com.sinya.shoppingmall.utils.ImageLoaderUtils;

import java.util.List;

/**
 * 新品发布
 *
 * @author KoizumiSinya
 * @date 2016/1/14.
 */
public class NewSaleFragment extends BaseFragment implements GetDataInterface {

    private GridView grid_view;
    private MyAdapter adapter;
    private ImageLoader imageLoader;

    private PullToRefreshBase.Mode pullState;

    private SwipeToLoadLayout pull_to_refresh;
    private List<ProductBean> mDataList;
    private int pageNum = 18;
    private int currentPage = 1;

    // [+] Handler

    Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what) {

                //下拉刷新获取数据
                case 0:
                    pull_to_refresh.setRefreshing(false);
                    if (msg.obj != null) {
                        BaseResult baseResult = (BaseResult) msg.obj;
                        if (baseResult.productList != null && baseResult.productList.size() > 0) {
                            mDataList = baseResult.productList;
                            adapter.setDataList(mDataList);
                        }
                    }

                    //上拉加载获取数据
                case 1:
                    //设置上拉加载完成
                    pull_to_refresh.setLoadingMore(false);
                    if (msg.obj != null) {
                        BaseResult baseResult = (BaseResult) msg.obj;
                        if (baseResult.productList != null && baseResult.productList.size() > 0) {
                            for (ProductBean b : baseResult.productList) {
                                if (!mDataList.contains(b)) {
                                    mDataList.add(b);
                                }
                            }
                            adapter.setDataList(mDataList);
                        }
                    }
                    currentPage++;
                    break;
                default:
                    break;
            }

            return false;
        }
    });

    // [-] Handler
    // [+] Override Method

    @Override
    protected View initView() {
        //TODO 必须继承的方法 用来返回布局文件
        return View.inflate(mContext, R.layout.fragment_discount, null);
    }

    @Override
    protected void setView() {
        //TODO 必须继承的方法 用来设置初始化布局控件
        initPullToRefresh();
        initGridView();

    }

    @Override
    protected void initData() {
        imageLoader = ImageLoader.getInstance();
    }

    @Override
    public void fristGetData() {
        //TODO 实现自GetDataInterface接口
        VolleyRequest.Start(mContext, handler, 0, VolleyParams.newproduct, String.valueOf(currentPage), String.valueOf(pageNum), SortType.SALEDOWN.getType());
    }

    @Override
    public void updateData() {
        //TODO 实现自GetDataInterface接口


    }

    @Override
    public void onResume() {
        super.onResume();
    }

    // [-] Override Method
    // [+] init

    private void initPullToRefresh() {
        pull_to_refresh = (SwipeToLoadLayout) rootView.findViewById(R.id.pull_to_refresh);
        //设置下拉刷新监听
        pull_to_refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                currentPage = 1;
                VolleyRequest.Start(mContext, handler, 0, VolleyParams.newproduct, String.valueOf(currentPage), String.valueOf(pageNum), SortType.SALEDOWN.getType());
            }
        });
        //设置上拉加载监听
        pull_to_refresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                VolleyRequest.Start(mContext, handler, 1, VolleyParams.newproduct, String.valueOf(currentPage + 1), String.valueOf(pageNum), SortType.SALEDOWN.getType());
            }
        });
    }

    private void initGridView() {
        grid_view = (GridView) rootView.findViewById(R.id.swipe_target);
        adapter = new MyAdapter(mDataList);
        grid_view.setAdapter(adapter);
    }


    // [-]init
    // [+] Adapter

    class MyAdapter extends BaseAdapter {

        private List<ProductBean> dataList;

        public MyAdapter(List<ProductBean> dataList) {
            this.dataList = dataList;
        }

        public void setDataList(List<ProductBean> dataList) {
            this.dataList = dataList;
            notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            if (dataList != null && dataList.size() > 0) {
                return dataList.size();
            }
            return 0;
        }

        @Override
        public ProductBean getItem(int position) {
            if (dataList != null && dataList.get(position) != null) {
                return dataList.get(position);
            }
            return null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView = View.inflate(mContext, R.layout.item_commodity_gridview, null);
            TextView name = (TextView) convertView.findViewById(R.id.tv_name);
            TextView price = (TextView) convertView.findViewById(R.id.tv_price);
            TextView marketPrice = (TextView) convertView.findViewById(R.id.tv_marketPrice);
            ImageView imageView = (ImageView) convertView.findViewById(R.id.img);

            ProductBean productBean = getItem(position);
            name.setText(productBean.name);
            price.setText("￥" + productBean.price);
            marketPrice.setText("￥" + productBean.marketPrice);
            price.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            imageLoader.displayImage(Constants.BASE_URL + productBean.pic, imageView, ImageLoaderUtils.options);
            return convertView;
        }

    }

    // [-] Adapter
}
